package com.fiberhome.ztelauncher.core.page.holder;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;


import com.fiberhome.zte_launcher.R;
import com.fiberhome.ztelauncher.core.page.MultiTypeAdapter;
import com.fiberhome.ztelauncher.core.page.model.ImageType;
import com.fiberhome.ztelauncher.util.BitmapUitl;
import com.fiberhome.ztelauncher.core.page.SwitchAnimView;
import com.fiberhome.ztelauncher.xml.bean.LaunchDataPage;

import java.util.List;


/**
 * Created by xc on 2018/3/27.
 */

public class ImageViewHolder extends BaseViewHolder<ImageType> implements BaseViewHolder.OnFocusListener,
        View.OnClickListener{

    private Context context;
    private SwitchAnimView sav;
    private ImageType imageType;
    private MultiTypeAdapter adapter;
    private int position;
    private List<LaunchDataPage.ElementData> listElementData;

    public ImageViewHolder(View itemView) {
        super(itemView);
    }

    @Override
    public void setUpView(ImageType imageType, int position, MultiTypeAdapter adapter) {
        context = itemView.getContext();
        this.imageType = imageType;
        this.position = position;
        this.adapter = adapter;
        listElementData = imageType.getDataElement().elementDatas.elementDatas;
        setItemViewParams(imageType);
        initView();
        itemView.setOnClickListener(this);
        adapter.setOnFocusListener(this);
    }

    private void initView() {
        sav = (SwitchAnimView) getView(R.id.sav);
        sav.setElementData(imageType);
        BitmapUitl.loadBackground(context, itemView, listElementData.get(0).backGroundContentURL);
        BitmapUitl.loadBitmap(context, sav.getIvFirst(), listElementData.get(0).contentURL);
    }

    @Override
    public void onFocus(BaseViewHolder baseViewHolder,boolean isFocus,int position) {
        if(baseViewHolder instanceof ImageViewHolder){
            ImageViewHolder ivh = (ImageViewHolder) baseViewHolder;
            LaunchDataPage.ElementData elementData = adapter.getModels().get(position).getDataElement().
                    elementDatas.elementDatas.get(ivh.sav.getIndex());
            if (!TextUtils.isEmpty(elementData.focusContentURL)) {
                if (isFocus) {
                    BitmapUitl.loadBitmap(context, ivh.sav.getIvFirst(), elementData.focusContentURL);
                } else {
                    BitmapUitl.loadBitmap(context, ivh.sav.getIvFirst(), elementData.contentURL);
                }
            }
        }
    }

    @Override
    public void onClick(View view) {
        adapter.clickElement(
                listElementData.get(sav.getIndex()).elementAction,
                listElementData.get(sav.getIndex()).extraData);
    }
}
